<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>鼠标跟随-图片</title>
</head>
<style>
    body {
            margin: 0;
            height: 100vh;
        }
    span {
        /* display: block; */
        width: 6px;
        height: 6px;
        background: red;
        transform: rotate(45deg);
        position: absolute;
        transition: all 1s;
        animation: xiaoshi 1s;
    }

    span::after {
        content: "";
        display: block;
        width: 6px;
        height: 6px;
        border-radius: 25px;
        position: absolute;
        top: 0px;
        left: -3px;
        background: red;
    }

    span::before {
        content: "";
        display: block;
        width: 6px;
        height: 6px;
        border-radius: 25px;
        position: absolute;
        top: -3px;
        right: 0;
        background: red;
    }

    @keyframes xiaoshi {
        0% {
            transform: scale(1);
        }

        50% {
            transform: scale(2);
            top: 20px;
        }

        100% {
            transform: scale(2);
            top: 30px;
        }
    }
</style>

<body>

    <script>
        /* 
            1. 获取鼠标 位置 坐标
            2. 创建 像素  动画
            3. 随机 像素 
            4. 赋值 坐标
        */

        var body = document.body;

        body.onmousemove = function (e) {  //监听 鼠标位置
            e = e || window.event  // 兼容 e
            var x = e.clientX;
            var y = e.clientY;
            // console.log('x:' + x, 'y:' + y);
            var span = document.createElement('span')  //  创建 span
            span.style.left = x + 'px'
            span.style.top = y + 'px'
            body.appendChild(span)

            setTimeout(function () {   // 延迟 更改 span 位置
                var num = getSuiJi();
                console.log(num);
                span.style.left = (x + num) + 'px'
                span.style.top = (y /* + 200 + num */) + 'px'
            }, 100)

            setTimeout(function() {   // 延迟删除 span 标签
                body.removeChild(span)
            }, 1000)
            
        }

        function getSuiJi() {    // 封装 随机数 
            var suiJi = Math.floor(Math.random() * 200) - 100
            return suiJi
            // console.log(suiJi);
        }


    </script>
</body>

</html>